package com.ck.chuangke.controller;

import com.ck.chuangke.service.CkDeviceModelService;
import com.ck.constant.DeviceModelConstant;
import com.ck.constant.ResponseConstant;
import com.ck.exception.ServiceException;
import com.ck.model.common.DeviceModel;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.common.vo.DeviceModelListVO;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName CkSystemController
 * @Description 创客管理后台-系统管理
 * @Author HanQinrui
 * @Date 2018/7/6 14:38
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/ckSystem")
public class CkSystemController {
    private Logger logger = LoggerFactory.getLogger(CkProgramController.class);
    @Autowired
    private CkDeviceModelService ckDeviceModelService;

    /**
     * 终端机型分页列表
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @param model
     * @return
     */
    @RequestMapping(value = "/listByPage")
    public Object listByPage(Integer pageNum, Integer pageSize, Model model) {
        Page<DeviceModelListVO> pageInfo = ckDeviceModelService.listDeviceModelByPage(pageNum, pageSize);
        model.addAttribute("deviceModelList", pageInfo.getResult());
        model.addAttribute("pageNum", pageInfo.getPageNum());
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("total", pageInfo.getTotal());
        return "ck/ck_deviceModel_list";
    }

    /**
     * 新增终端机型
     * @param deviceModel 终端机型信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDeviceModel")
    public Object addDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        if(deviceModel == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "参数不能为空");
        }
        if(org.apache.commons.lang3.StringUtils.isBlank(deviceModel.getModelName())) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "参数不能为空");
        }
        try {
            // 重复性验证
            String modelName = deviceModel.getModelName();
            DeviceModel deviceModelRepeat =  ckDeviceModelService.getDeviceModelByName(modelName);
            if(deviceModelRepeat != null) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "终端型号已存在，请重新输入");
            }
            ckDeviceModelService.addDeviceModel(deviceModel, request);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkSystemController addDeviceModel 新增终端机型失败：" + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, "新增终端机型失败");
        }
        return "";
    }

    /**
     * 删除终端型号
     * @param deviceModelIds 终端型号Id(多个Id之间用逗号隔开 )
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public Object deleteDeviceModel(String deviceModelIds, HttpServletRequest request) {
        if(StringUtils.isBlank(deviceModelIds)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, "ID不能为空");
        }
        try {
            ckDeviceModelService.deleteDeviceModelBatch(deviceModelIds, request);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkSystemController deleteDeviceModelBatch 批量删除终端型号失败：" + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, "批量删除终端型号失败");
        }
        return "";
    }

    /**
     * 根据主键获取单个机型
     *
     * @param deviceModelId 终端机型信息
     * @return Object
     * @author linqiang
     */
    @ResponseBody
    @RequestMapping(value = "/getDeviceModelById")
    public Object getDeviceModelById(Long deviceModelId) {
        if (deviceModelId == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        try {
            DeviceModel deviceModel = ckDeviceModelService.getDeviceModelById(deviceModelId);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, deviceModel, DeviceModelConstant.MSG_SELECT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkSystemController getDeviceModelById 获取终端机型失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_SELECT_FAIL);
    }

    /**
     * 修改终端机型
     *
     * @param deviceModel 终端机型信息
     * @return Object
     * @author linqiang
     */
    @ResponseBody
    @RequestMapping(value = "/updateDeviceModel")
    public Object updateDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        if (deviceModel == null || deviceModel.getDeviceModelId() == null) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_NO_PARAM);
        }
        if (StringUtils.isBlank(deviceModel.getModelName())) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_DEVICE_MODEL_NOT_NULL);
        }
        if(StringUtils.isNotBlank(deviceModel.getRemark())){
            if(deviceModel.getRemark().length() > DeviceModelConstant.MAX_REMARK_LENGTH){
                return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_REMARK_LENGTH_OVER);
            }
        }
        try {
            ckDeviceModelService.updateByPrimaryKeySelective(deviceModel, request);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, DeviceModelConstant.MSG_UPDATE_DDEVICE_MODEL_SUCCESS);
        } catch (ServiceException e1) {
            e1.printStackTrace();
            logger.error("CkSystemController updateDeviceModel 修改终端机型失败：" + e1.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e1.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CkSystemController updateDeviceModel 修改终端机型失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, DeviceModelConstant.MSG_UPDATE_DDEVICE_MODEL_FAIL);
    }
}
